#!/usr/bin/env ruby

module RedBook
	TEST_HOME = File.dirname(File.expand_path(__FILE__))+'/..' unless const_defined?(:TEST_HOME)
end

require "#{RedBook::TEST_HOME}/lib/redbook"

RedBook::CONFIG = "#{RedBook::TEST_HOME}/test/rbtestconfig.yml"



describe RedBook::Parser do
	
	before :each do
		@now = Time.local( 2008, 1, 1, 12, 0, 0) # Jan 1st 2008 at 12:00:00 PM
		@parser = RedBook::Parser.new(@now)
	end
	
	it "should parse :log commands with correctly-formatted tags." do
		@parser.parse ":log this is a test :with tag1 tag2"
		@parser.command.should == {:operation => :log, :message => 'this is a test', :tags => ['tag1', 'tag2']}
	end
	
	it "should throw an error for illegal tags when logging a message." do
		lambda {@parser.parse(":log this is a test :with tag1 !tag2")}.should raise_error(ParserError)	
	end
	
	it "should parse simple :select commands" do
		@parser.parse ":select :pending :from today"
		@parser.command.should == {:operation => :select, :pending => true, :from => Time.local( 2008, 1, 1, 6, 0, 0)}
		@parser.parse ":select :last 5 :until two days ago"
		@parser.command.should == {:operation => :select, :last => 5, :to => Time.local( 2007, 12, 30, 12, 0, 0)}
	end
	
	it "should parse complex :select commands" do
		@parser.parse ":select :plain :since 3 weeks ago :with mail !personal"
		@parser.command.should == {:operation => :select, :plain => true,
															 :from => Time.local( 2007, 12, 11, 12, 0, 0),
															 :tags => [{:positive => ['mail'], :negative => ['personal']}] }
		@parser.parse ":select :completed :with mail !personal | mail work !important"
		@parser.command.should == {:operation => :select, :completed => true,
															 :tags => [{:positive => ['mail'], :negative => ['personal']},
																				 {:positive => ['mail', 'work'], :negative => ['important']}] }
	end
	
	it "should escape colons via :-" do
		@parser.parse ":log Testing escape on directives like :-select"
		@parser.command.should == {:operation => :log, :message => "Testing escape on directives like :select"}
	end
	
	it "should parse :delete, :update, :calc and :relog commands" do
		@parser.parse ":update 23 :with tag1 :message Updated message"
		@parser.command.should == {:operation => :update, :message => "23", :text => "Updated message", :tags => ['tag1']}
		@parser.parse ":relog 2"
		@parser.command.should == {:operation => :relog, :message => "2"}
		@parser.parse ":delete something"
		@parser.command.should == {:operation => :delete, :selected => []}
		@parser.parse ":delete 2 3 4 6"
		@parser.command.should == {:operation => :delete, :selected => [2,3,4,6]}
		@parser.parse ":calc 7 3 4 6"
		@parser.command.should == {:operation => :calc, :selected => [7,3,4,6]}
	end
	
	it "should parse :set commands" do
		@parser.parse ":set m_test :value :-select :-pending :-from :-%today"
		@parser.command.should == {:operation => :set, :value => ":select :pending :from :%today", :message => "m_test"}
	end
	
	it "should parse variables" do
		@parser.parse ":select /test/i :pending :from :%monday"
		@parser.command.should == {:operation => :select, :message => "/test/i", :pending => true, :from => Time.local( 2007, 12, 31, 8, 0, 0)}
	end
	
	it "should parse macros" do
		@parser.parse ":status Testing macros :with test"
		@parser.command.should == {:operation => :log, :message => "Testing macros", :tags => ['test','_twitter']}
	end
		
end
